<template>
  <div class="app-container form_body">
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="请选择部门" prop="pid"  >
    <el-select v-model="ruleForm.pid" placeholder="请选择部门">
          <el-option
      v-for="item in options"
      :key="item.id"
      :label="item.pname"
      :value="item.id">
    </el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="用户名称" prop="username">
    <el-input v-model="ruleForm.username"></el-input>
  </el-form-item>
    <el-form-item label="手机号:" prop="mobile">
    <el-input v-model="ruleForm.mobile"></el-input>
  </el-form-item>
  <el-form-item label="密码:" prop="password">
    <el-input v-model="ruleForm.password"></el-input>
  </el-form-item>
 

  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">立即添加</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      options: [],
      ruleForm: {
        username: '',
        mobile: '',
        password: '',
        pid: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 1, max: 55, message: '长度在 1 到 55 个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        mobile: [
          {
            required: true,
            min: 1,
            max: 15,
            message: '请输入手机号',
            trigger: 'blur'
          }
        ],
        partment: [{ required: true, message: '请选择部门' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      var that = this
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(that.ruleForm)
          that.addAdmin(that.ruleForm)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    addAdmin(data) {
      var that = this

      that
        .$request({
          url: '/admin/addadmin',
          method: 'post',
          data
        })
        .then(res => {
          console.log(res)
          if (res.code == 200) {
            this.$notify({
              title: '成功',
              message: '添加成功',
              type: 'success',
              duration: 900
            })
           that.ruleForm = '';
            this.$refs[formName].resetFields()
          }
        })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    getPartment() {
      var that = this
      that
        .$request({
          url: '/admin/get_department',
          method: 'post'
        })
        .then(res => {
          if (res.code == 200) {
            that.options = res.data
          }
        })
    }
  },
  mounted() {
    this.getPartment()
  }
}
</script>
<style  scode>
.form_body {
  width: 330px;
  height: auto;
}
</style>
